package com.twu.biblioteca.content;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by mduemcke on 9/25/14.
 */
public abstract class ContentAbstract<T> {

    protected List<T> availableItems;
    protected List<T> borrowedItems;

    public ContentAbstract() {
        fillContent();
        borrowedItems = new LinkedList<T>();
    }

    public void listItems() {
        System.out.print(listToString(availableItems));
    }

    public void checkoutItem(String title, String successMessage, String failureMessage) {
        int ind = getIndexFromTitle(title, availableItems);
        if (ind != -1) {
            borrowedItems.add(availableItems.get(ind));
            availableItems.remove(ind);
            System.out.println(successMessage);
        } else {
            System.out.println(failureMessage);
        }
    }

    public void returnItem(String title, String successMessage, String failureMessage) {
        int ind = getIndexFromTitle(title, borrowedItems);
        if (ind != -1) {
            T b = borrowedItems.get(ind);
            availableItems.add(b);
            borrowedItems.remove(ind);
            System.out.println(successMessage);
        } else {
            System.out.println(failureMessage);
        }
    }

    protected abstract void fillContent();

    protected abstract String listToString(List<T> l);

    abstract int getIndexFromTitle(String title, List<T> list);

    public List<T> getAvailableItems() {
        return availableItems;
    }
}
